<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <!-- 轮播图 -->
      <div class="swiper-slide" v-for="img in imageList" :key="img.id">
        <router-link :to="img.linkUrl">
          <img :src="img.imageUrl" :alt="img.title" />
        </router-link>
      </div>
    </div>
    <!-- 左右箭头 -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
    <!-- 小圆点 -->
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";

export default {
  name: "Carousel",
  props: {
    imageList: { type: Array, required: true },
  },
  // mounted() {},
  watch: {
    imageList() {
      this.$nextTick(() => {
        // 等DOM元素更新好，再触发
        new Swiper(".swiper-container", {
          navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
          },
          pagination: {
            el: ".swiper-pagination",
          },
          loop: true,
          autoplay: true,
        });
      });
    },
  },
};
</script>

<style>
</style>